package com.atguigu.tingshu.album.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.alibaba.fastjson.JSONObject;
import com.atguigu.tingshu.album.mapper.*;
import com.atguigu.tingshu.album.service.BaseCategoryService;
import com.atguigu.tingshu.common.execption.GuiguException;
import com.atguigu.tingshu.model.album.*;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

@Service
@SuppressWarnings({"all"})
public class BaseCategoryServiceImpl extends ServiceImpl<BaseCategory1Mapper, BaseCategory1> implements BaseCategoryService {

	@Autowired
	private BaseCategory1Mapper baseCategory1Mapper;

	@Autowired
	private BaseCategory2Mapper baseCategory2Mapper;

	@Autowired
	private BaseCategory3Mapper baseCategory3Mapper;

    @Autowired
    private BaseCategoryViewMapper baseCategoryViewMapper;

    @Autowired
    private BaseAttributeMapper baseAttributeMapper;




    /**
     * 根据一级分类id获取全部分类信息
     * /api/album/category/getBaseCategoryList/{category1Id}
     */
    @Override
    public JSONObject getBaseCategoryList(Long category1Id) {
        //根据一级分类id获取数据
        LambdaQueryWrapper<BaseCategoryView> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(BaseCategoryView::getCategory1Id,category1Id);
        List<BaseCategoryView> list1 = baseCategoryViewMapper.selectList(lambdaQueryWrapper);
        //判断
        if (CollectionUtil.isEmpty(list1)){
            throw new GuiguException(400,"搜索查询所有分类--出现错误");
        }
        //过滤
        Map<Long, List<BaseCategoryView>> map1 = list1.stream().collect(Collectors.groupingBy(BaseCategoryView::getCategory1Id));
//创建封装一级分类对象
        JSONObject jso1 = new JSONObject();
        for (Map.Entry<Long, List<BaseCategoryView>> for1 : map1.entrySet()) {


            //一级分类id
            Long category1Id1 = list1.get(0).getCategory1Id();
            //一级分类name
            List<BaseCategoryView> value = for1.getValue();
            String category1Name = value.get(0).getCategory1Name();
            //封装
            jso1.put("categoryName",category1Name);
            jso1.put("categoryId",category1Id1);
            //获取二级分类
            List<JSONObject> list2 = new ArrayList<>();
            Map<Long, List<BaseCategoryView>> map2 = list1.stream().collect(Collectors.groupingBy(BaseCategoryView::getCategory2Id));
            for (Map.Entry<Long, List<BaseCategoryView>> longListEntry : map2.entrySet()) {
                JSONObject jso2 = new JSONObject();
                //获取二级分类id
                Long key = longListEntry.getKey();
                //获取二级分类name
                List<BaseCategoryView> value2 = longListEntry.getValue();
                String category2Name = value2.get(0).getCategory2Name();
                //封装
                jso2.put("categoryName",category2Name);
                jso2.put("categoryId",key);

                //获取三级分类
                List<JSONObject> collect = value2.stream().map(baseCategoryView -> {
                    //收集三级id
                    JSONObject jso3 = new JSONObject();
                    //获取三级id
                    Long category3Id = baseCategoryView.getCategory3Id();
                    //获取三级name
                    String category3Name = baseCategoryView.getCategory3Name();
                    //封装
                    jso3.put("categoryName", category3Name);
                    jso3.put("categoryId", category3Id);
                    return jso3;
                }).collect(Collectors.toList());
                jso2.put("categoryChild",collect);
                list2.add(jso2);
            }
            jso1.put("categoryChild",list2);
        }
        return jso1;
    }



    /**
     * 根据一级分类Id查询三级分类列表
     * /api/album/category/findTopBaseCategory3/{category1Id}
     */
    @Override
    public List<BaseCategory3> findTopBaseCategory3(Long category1Id) {
        //根据一级分类id查询二级分类集合
        LambdaQueryWrapper<BaseCategory2> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(BaseCategory2::getCategory1Id,category1Id);
        List<BaseCategory2> baseCategory2s = baseCategory2Mapper.selectList(lambdaQueryWrapper);

        //根据二级分类集合，封装id
        List<Long> collect = baseCategory2s.stream().map(baseCategory2 -> baseCategory2.getId()).collect(Collectors.toList());
        //查询三级分类
        LambdaQueryWrapper<BaseCategory3> lambdaQueryWrapper1 = new LambdaQueryWrapper<>();
        lambdaQueryWrapper1.in(BaseCategory3::getCategory2Id,collect);
        lambdaQueryWrapper1.eq(BaseCategory3::getIsTop,1);
        lambdaQueryWrapper1.orderByDesc(BaseCategory3::getOrderNum);
        lambdaQueryWrapper1.last("limit 7");
        List<BaseCategory3> list = baseCategory3Mapper.selectList(lambdaQueryWrapper1);

        return list;
    }




    /**
     * 查询所有分类（1、2、3级分类）
     * /api/album/category/getBaseCategoryList
     * @return
     */
    @Override
    public List<JSONObject> getBaseCategoryList() {
        //获取所有数据..这个表是一个视图表
        List<BaseCategoryView> baseCategoryViews = baseCategoryViewMapper.selectList(null);
        if (CollectionUtil.isEmpty(baseCategoryViews)){
            throw new GuiguException(400,"查询所有分类--出现错误");
        }
        //收集一级分类
        List<JSONObject> list1 = new ArrayList<>();
        //获取一级分类是多个一级分类
        Map<Long, List<BaseCategoryView>> map1Id = baseCategoryViews.stream().collect(Collectors.groupingBy(BaseCategoryView::getCategory1Id));
        //增强for一个一个遍历
        for (Map.Entry<Long, List<BaseCategoryView>> longListEntry : map1Id.entrySet()) {
            //这个用来封装一级分类
            JSONObject obj1 = new JSONObject();
            //获取一级分类id
            Long id1 = longListEntry.getKey();
            obj1.put("categoryId",id1);
            //获取一级分类name
            List<BaseCategoryView> value = longListEntry.getValue();
            String category1Name = value.get(0).getCategory1Name();
            obj1.put("categoryName",category1Name);

            //开始二级内容
            //收集所有二级分类存入一级分类
            List<JSONObject> list2 = new ArrayList<>();
            //首先便利出所有二级,它是所有的二级分类，让后用增强for便利一个一个的，放入三级
            Map<Long, List<BaseCategoryView>> map2Id = value.stream().collect(Collectors.groupingBy(BaseCategoryView::getCategory2Id));
            //这个便利出了一个一个的二级分类
            for (Map.Entry<Long, List<BaseCategoryView>> listEntry : map2Id.entrySet()) {
                //这个用来封装二级分类
                JSONObject obj2 = new JSONObject();
                //获取二级分类id存入
                Long id2 = listEntry.getKey();
                obj2.put("categoryId",id2);
                //获取二级分类的name
                List<BaseCategoryView> value2 = listEntry.getValue();
                String category2Name = value2.get(0).getCategory2Name();
                obj2.put("categoryName",category2Name);

                //开始三级内容
                List<JSONObject> list3 = value2.stream().map(baseCategoryView -> {
                    //收集三级内容
                    JSONObject obj3 = new JSONObject();
                    //获取三级分类id
                    Long category3Id = baseCategoryView.getCategory3Id();
                    obj3.put("categoryId", category3Id);
                    //获取三级分类name
                    String category3Name = baseCategoryView.getCategory3Name();
                    obj3.put("categoryName", category3Name);
                    return obj3;
                }).collect(Collectors.toList());
                //结束三级内容，会收到封装好的list3存入二级分类
                obj2.put("categoryChild",list3);
                //结束一轮二级分类存入list中
                list2.add(obj2);
            }
            //将二级收集好的传入进去
            obj1.put("categoryChild",list2);
            list1.add(obj1);
        }
        return list1;
    }

    @Override
    public List<BaseAttribute> findAttribute(Long category1Id) {
        return baseAttributeMapper.selectAttribute(category1Id);
    }

    /**
     * 根据三级分类Id 获取到分类信息
     * /api/album/category/getCategoryView/{category3Id}
     */
    @Override
    public BaseCategoryView getCategoryView(Long category3Id) {
        LambdaQueryWrapper<BaseCategoryView> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(BaseCategoryView::getCategory3Id,category3Id);
        BaseCategoryView baseCategoryView = baseCategoryViewMapper.selectOne(lambdaQueryWrapper);
        return baseCategoryView;
    }




}
